import tkinter as tk
from tkinter import messagebox
import requests
from bs4 import BeautifulSoup
 
def get_movie_info(movie_title):
    url = f"https://www.iqiyi.com/v_search?q={movie_title}" 
    try:
        response = requests.get(url) 
        response.raise_for_status() 
        soup = BeautifulSoup(response.text,  'html.parser') 
        
        search_results = soup.find_all('li',  class_='item')
        if search_results:
            first_result = search_results[0]
            title = first_result.find('a',  class_='title').text.strip() 
            link = "https:" + first_result.find('a')['href'] 
            return {'title': title, 'link': link}
        else:
            return None
    except requests.RequestException as e:
        messagebox.showerror(" 请求错误", f"{e}")
        return None
 
def get_music_info(song_title):
    url = f"https://music.163.com/search?keywords={song_title}&type=1" 
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
    }
    try:
        response = requests.get(url,  headers=headers)
        response.raise_for_status() 
        soup = BeautifulSoup(response.text,  'html.parser') 
        
        song_list = soup.find('ul',  id='m-song-module')
        songs = song_list.find_all('li')  if song_list else []
        
        if songs:
            first_song = songs[0]
            song_name = first_song.find('b',  class_='txt').text.strip() 
            singer = first_song.find('span',  class_='srch_singer').text.strip() 
            song_link = "https:" + first_song.find('a')['href'] 
            return {'song_name': song_name, 'singer': singer, 'link': song_link}
        else:
            return None
    except requests.RequestException as e:
        messagebox.showerror(" 请求错误", f"{e}")
        return None
 
def search_button_click():
    movie_title = movie_entry.get() 
    music_title = music_entry.get() 
 
    movie_info = get_movie_info(movie_title)
    music_info = get_music_info(music_title)
 
    result_label.config(text="") 
    if movie_info:
        result_label.config(text=f" 电影信息:\n标题: {movie_info['title']}\n链接: {movie_info['link']}\n\n")
    else:
        result_label.config(text=" 未找到该电影。\n\n")
 
    if music_info:
        result_label.config(text=result_label.cget('text')  + f"音乐信息:\n歌曲名称: {music_info['song_name']}\n歌手: {music_info['singer']}\n链接: {music_info['link']}\n")
    else:
        result_label.config(text=result_label.cget('text')  + "未找到该音乐。\n")
 
# 创建主窗口
root = tk.Tk()
root.title(" 电影和音乐查询")
 
# 设置窗口大小 
root.geometry("600x400") 
root.configure(bg="#f0f0f0")   # 背景色
 
# 定义字体
font = ("Arial", 12)
 
# 创建标签和输入框
movie_label = tk.Label(root, text="电影名称:", font=font, bg="#f0f0f0")
movie_label.pack(pady=10) 
 
movie_entry = tk.Entry(root, width=50, font=font)
movie_entry.pack(pady=5) 
 
music_label = tk.Label(root, text="音乐名称:", font=font, bg="#f0f0f0")
music_label.pack(pady=10) 
 
music_entry = tk.Entry(root, width=50, font=font)
music_entry.pack(pady=5) 
 
# 创建按钮
search_button = tk.Button(root, text="查询", command=search_button_click, font=font, bg="#4CAF50", fg="white")
search_button.pack(pady=10) 
 
# 结果显示区域
result_label = tk.Label(root, text="", wraplength=550, justify="left", font=("Arial", 10), bg="#f0f0f0")
result_label.pack(pady=10) 
 
# 进入主循环
root.mainloop() 